package com.example._06blog.controller;

import com.example._06blog.pojo.User;
import com.example._06blog.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

/**
 * @auther 0785fanlei
 * @date 2022/4/1 19:22
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/")
    public String index(){
        return "user/index";
    }

    @GetMapping("/login")
    public String login(){
        return "user/login";
    }

    @GetMapping("loginchk")
    public String loginchk(String username, String password, HttpSession session){


        User u=userService.login(username,password);
        System.out.println(u);
        if(u!=null){
            session.setAttribute("user",u);//登录信息放在会话中是为了跳转页面时保存信息
            return "redirect:/user/";
        }else {
            return "redirect:/user/login";
        }
    }
}
